package com.his.dictrole.controller;

import com.his.common.verification.group.Update;
import com.his.config.annotation.CurrentUser;
import com.his.dictrole.service.DictRoleEmpService;
import com.his.dictrole.service.DictRoleService;
import com.his.dictrole.vo.DictRoleEmpVo;
import com.his.dictrole.vo.DictRoleMenuVo;
import com.his.dictrole.vo.DictRoleVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author xiaoxiaoping
 * @Date 2020/3/9 14:06
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/api/role")
public class DictRoleController extends ResultBaseController {

    private  final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @Resource
    private DictRoleService dictRoleService;
    @Resource
    private DictRoleEmpService dictRoleEmpService;

    @PostMapping(value = "/add.do")
    public ResultUtil saveRole(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DictRoleVo dictRoleVo){
        try{
            dictRoleService.save(dictRoleVo,dictEmployee);
            return this.success("保存成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value = "/get_all.do")
    public ResultUtil queryAll(@CurrentUser DictEmployee dictEmployee){
        try{
            return this.success("获取成功！", dictRoleService.qureyAll());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @GetMapping(value = "/get_by_id.do")
    public ResultUtil getById(@CurrentUser DictEmployee dictEmployee,String id){
        try{
            return this.success("获取成功！", dictRoleService.selectById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value = "/delete_by_id.do")
    public ResultUtil deleteById(@CurrentUser DictEmployee dictEmployee,String id){
        try{
            dictRoleService.deleteById(id);
            return this.success("获取成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value = "/role_emp.do")
    public ResultUtil reole_emp(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DictRoleEmpVo dictRoleEmpVo){
        try{
            dictRoleEmpService.save(dictRoleEmpVo);
            return this.success("保存成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value="/role_menu.do")
    public ResultUtil roleBindMenu(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DictRoleMenuVo dictRoleMenuVo){
        try{
            dictRoleService.roleBindMenu(dictRoleMenuVo);
            return this.success("设置成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value="/update.do")
    public ResultUtil updateRoleById(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated({Update.class}) DictRoleVo dictRoleVo){
        try{
            dictRoleService.updateById(dictEmployee,dictRoleVo);
            return this.success("修改成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value = "/get_show.do")
    public ResultUtil getShowRole(@CurrentUser DictEmployee dictEmployee){
        try{
            return this.success("",dictRoleService.getShowRole());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
}
